/* 
 * File:   MyBatchMergeModel.cpp
 * Author: Morpheus
 * 
 * Created on 20 juin 2013, 23:40
 */

#include "MyBatchMergeModel.h"
#include <QtCore/qdatastream.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qvariant.h>
#include <QtCore/qdebug.h>
#include <QtGui/qcolor.h>
#include <QtGui/qfont.h>
#include <QtGui/qicon.h>

MyBatchMergeModel::MyBatchMergeModel(QObject* parent)
 : QAbstractTableModel(parent), m_curBatchId(-1) {
}

MyBatchMergeModel::~MyBatchMergeModel() {
}
void MyBatchMergeModel::setBatchs(const QList<MyBatchMerge>& list){
    emit layoutAboutToBeChanged();
    m_dataList = list;
    emit layoutChanged();
}
void MyBatchMergeModel::appendSeveralBatch(const QByteArray& array){
    QDataStream out(array);
    QList<MyBatchMerge> batchs;
    out >> batchs;
    if(out.status() == QDataStream::ReadCorruptData)
        return;
    
    emit layoutAboutToBeChanged();
    foreach(MyBatchMerge batch, batchs){
        int idx = -1;
        for(int i = 0; i < m_dataList.size(); i++){
            if(m_dataList[i].getUUID() == batch.getUUID()){
                idx = i;
                break;
            }
        }
        if(idx != -1){
            m_dataList.replace(idx, batch);
        }else{
            m_dataList << batch;
        }
    }
    emit layoutChanged();
}
void MyBatchMergeModel::appendBatch(MyBatchMerge batch){
    batch.detach();
    int idx = -1;
    for(int i = 0; i < m_dataList.size(); i++){
        if(m_dataList[i].getUUID() == batch.getUUID()){
            idx = i;
            break;
        }
    }
    emit layoutAboutToBeChanged();
    if(idx != -1){
        m_dataList.replace(idx, batch);
    }else{
        m_dataList << batch;
    }
    emit layoutChanged();
}

void MyBatchMergeModel::removeBatch(const QList<int>& uuids){
    emit layoutAboutToBeChanged();
    foreach(int elem, uuids){
        int idx = -1;
        for(int i = 0; i < m_dataList.size(); i++){
            if(m_dataList[i].getUUID() == elem){
                idx = i;
                break;
            }
        }

        if(idx != -1){
            m_dataList.removeAt(idx);
        }
    }
    emit layoutChanged();
}

QList<MyBatchMerge> MyBatchMergeModel::getAllBatch() const{
    return m_dataList;
}
QByteArray MyBatchMergeModel::getAllBatchArray() const{
    QByteArray array;
    QDataStream out(&array, QIODevice::WriteOnly);
    
    out << m_dataList;
    
    return array;
}
QByteArray MyBatchMergeModel::getBatchArray(const int& uuid) const{
    QByteArray array;
    QDataStream out(&array, QIODevice::WriteOnly);
    
    for(int i = 0; i < m_dataList.size(); i++){
        if(m_dataList[i].getUUID() == uuid){
            out << m_dataList[i];
            break;
        }
    }
    
    return array;
}
QModelIndex MyBatchMergeModel::getBatchIndex(const int& uuid) const{
    for(int i = 0; i < m_dataList.size(); i++){
        if(m_dataList[i].getUUID() == uuid){
            return index(i, 0);
        }
    }
    
    return index(-1, 0);
}

MyBatchMerge MyBatchMergeModel::getBatch(const int& uuid) const{
    MyBatchMerge batch;
    for(int i = 0; i < m_dataList.size(); i++){
        if(m_dataList[i].getUUID() == uuid){
            batch = m_dataList[i];
            break;
        }
    }
    return batch;
}

MyBatchMerge MyBatchMergeModel::getBatch(const QModelIndex& index) const{
    MyBatchMerge batch;
    if(!index.isValid())
        return batch;
    
    QByteArray array;
    {
        QDataStream out(&array, QIODevice::WriteOnly);
        out << m_dataList.at(index.row());
    }
    {
        QDataStream in(array);
        in >> batch;
    }
    
    return batch;
}
    
int MyBatchMergeModel::getCurBatchMerge() const{
    return m_curBatchId;
}
void MyBatchMergeModel::setCurBatchMerge(const int& s){
    emit layoutAboutToBeChanged();
    m_curBatchId = s;
    emit layoutChanged();
}

int MyBatchMergeModel::rowCount(const QModelIndex& parent) const{
    if(parent.isValid())
        return 0;
    return m_dataList.size();
}
int MyBatchMergeModel::columnCount(const QModelIndex& /*parent*/) const{
    return 1;
}
QVariant MyBatchMergeModel::data(const QModelIndex& index, int role) const{
    if(!index.isValid())
        return QVariant();
    
    if(role == Qt::DisplayRole){
        return m_dataList[index.row()].getName();
    }else if(role == Qt::DecorationRole){
        if(m_dataList[index.row()].getDoWithSuccess()){
            return QIcon(":/res/clean.png");
        }
    }else if(role == Qt::BackgroundColorRole){
        if(m_dataList[index.row()].getUUID() == m_curBatchId)
            return QColor("orange");
    }else if(role == Qt::FontRole){
        if(m_dataList[index.row()].getUUID() == m_curBatchId){
            QFont font;         font.setBold(true);
            return font;
        }
    }else if(role == Qt::ToolTipRole){
        QString text = tr("<h3>%1</h3>").arg(m_dataList[index.row()].getName());
        text += tr("<p>Création le : %1</p>").arg(m_dataList[index.row()].getCreatedDate().toString("dd MMMM yyyy hh:mm"));
        text += tr("<p>Modification le : %1</p>").arg(m_dataList[index.row()].getLastModifiedDate().toString("dd MMMM yyyy hh:mm"));
        return text;
    }
    
    return QVariant();
}
